package m202405.d14;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/14 14:03
 */
public class Q35 {

    public static void main(String[] args) {
        Q35 q35 = new Q35();
        q35.searchInsert(new int[]{1}, 1);
    }

    public int searchInsert(int[] nums, int target) {
        return find(nums, 0, nums.length, target);
    }

    public int find(int[] nums, int start, int end, int target) {
        if (start == end - 1) {
            if (target < nums[start]) {
                return start;
            } else if (target > nums[start]) {
                return start + 1;
            }else {
                return start;
            }
        }
        int mid = (start + end) / 2;
        if (target < nums[mid]) {
            return find(nums, start, mid, target);
        } else if (target > nums[mid]) {
            return find(nums, mid, end, target);
        } else {
            return mid;
        }
    }
}
